import dt from '@dt/dt'
import moment from 'moment'
import Opt from '@/components/dt/info/Opt.js'
import searchOpt from '../opt/search.js'
import channelTypeMgr from '@/biz/option/channelType'
import table from '@/components/dt/table/render.js'
let _this = null
class Column extends Opt {

  constructor(vm, status) {
    super()
    this.vm = vm
    _this = vm
    this.status = status
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.calculateDate = [
    {
      field: 'calculateDate',
      width: 150,
      cell: {
        value: (row) => row.calculateDate ? moment(row.calculateDate).format(
          'YYYY-MM-DD') : '',
        text: {}
      },
      header: {
        filter: {
          title: '本次结算日期',
          key: 'calculateDateStart',
          placeholder: '请选择日期',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.calculateDateStart && info.calculateDateEnd) {
                return [info.calculateDateStart, info.calculateDateEnd]
              }
              return null
            },
            to: (v) => {
              return {
                calculateDateStart: v[0],
                calculateDateEnd: v[1]
              }
            }
          }
        }
      },
    }]

    this.all = [{
      field: 'companyName',
      title: '所属公司',
      width: 150
    },
    {
      field: 'buildingName',
      title: '所属案场',
      width: 160
    },
    {
      field: 'productName',
      title: '所属产品',
      width: 160,
    },
    {
      field: 'roomName',
      title: '房源名称',
      width: 180
    },
    {
      field: 'channelName',
      title: '来源渠道',
      width: 150
    },
    {
      field: 'cooperationType',
      title: '合作类型',
      width: 150,
      header: {
        filter: channelTypeMgr.create({
          key: 'cooperationType',
        }),
      },
      cell: {
        text: {
          type: 'dict',
          key: 'cooperationTypeFinal'
        }
      }
    },
    {
      field: 'orderCode',
      title: '订单编号',
      width: 160,
      header: {
        filter: {
          key: 'orderCode',
          title: '订单编号',
          placeholder: '请输入认购或签约单编号',
          input: {}
        }
      }
    },
    {
      field: 'orderState',
      title: '订单状态',
      width: 100,
      cell: {
        value: (row) => dt.dictInfo().__value('riskOrderState', row.orderState),
        text: {}
      },
      header: {
        filter: {
          key: 'orderState',
          title: '订单状态',
          select: {
            key: 'riskOrderState',
            clearable: true
          }
        }
      }
    },
    {
      field: 'price',
      title: '成交总价(元)',
      width: 160,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'hisFirstTime',
      width: 130,
      cell: {
        value: (row) => row.hisFirstTime ? moment(row.hisFirstTime).format(
          'YYYY-MM-DD') : '',
        text: {}
      },
      header: {
        filter: {
          title: '首访日期',
          key: 'firstVisitStart',
          placeholder: '请选择日期',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.firstVisitStart && info.firstVisitEnd) {
                return [info.firstVisitStart, info.firstVisitEnd]
              }
              return null
            },
            to: (v) => {
              return {
                firstVisitStart: v[0],
                firstVisitEnd: v[1]
              }
            }
          }
        }
      },
    },
    {
      field: 'subscriptionDate',
      width: 130,
      cell: {
        value: (row) => row.subscriptionDate ? moment(row.subscriptionDate).format(
          'YYYY-MM-DD') : '',
        text: {}
      },
      header: {
        filter: {
          title: '认购日期',
          key: 'subscriptionStart',
          placeholder: '请选择日期',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.subscriptionStart && info.subscriptionEnd) {
                return [info.subscriptionStart, info.subscriptionEnd]
              }
              return null
            },
            to: (v) => {
              return {
                subscriptionStart: v[0],
                subscriptionEnd: v[1]
              }
            }
          }
        }
      },
    },
    {
      field: 'contractDate',
      width: 130,
      cell: {
        value: (row) => row.contractDate ? moment(row.contractDate).format(
          'YYYY-MM-DD') : '',
        text: {}
      },
      header: {
        filter: {
          title: '签约日期',
          key: 'contractStart',
          placeholder: '请选择日期',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.contractStart && info.contractEnd) {
                return [info.contractStart, info.contractEnd]
              }
              return null
            },
            to: (v) => {
              return {
                contractStart: v[0],
                contractEnd: v[1]
              }
            }
          }
        }
      },
    },
    {
      field: 'customerName',
      width: 120,
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {}
        }
      }
    },
    {
      field: 'customerPhone',
      width: 140,
      header: {
        filter: {
          title: '客户手机号',
          key: 'customerPhone',
          placeholder: '请输入手机号码',
          input: {}
        }
      }
    },
    {
      field: 'userName',
      width: 135,
      cell: {
        value: (row) => `${row.userName}(${row.workNo})`,
        text: {}
      },
      header: {
        filter: {
          title: '归属顾问',
          key: 'userName',
          placeholder: '请输入归属顾问',
          input: {}
        }
      }
    },
    {
      field: 'channelCost',
      title: '渠道费用(元)',
      width: 150,
      cell: (row) => {
        if (row.counting) {
          return {
            edit: {
              key: 'channelPrice',
              placeholder: '请输入渠道费用',
              input: {
                type: 'number'
              }
            }
          }
        } else {
          return {
            text: {
              type: 'number'
            }
          }
        }
      }
    },
    {
      field: 'settledAmount',
      title: '已结算金额(元)',
      width: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'thisAmount',
      title: '本次结算金额(元)',
      width: 150,
      cell: (row) => {
        if (row.counting) {
          return {
            edit: {
              key: 'countPrice',
              placeholder: '请输入渠道费用',
              input: {
                type: 'number'
              }
            }
          }
        } else {
          return {
            text: {
              type: 'number'
            }
          }
        }
      }
    }]
    if (this.status != '1') {
      this.all = [...this.all, ...this.calculateDate]
    }
    this.all.push({
      field: 'surplusAmount',
      title: '剩余应付金额(元)',
      width: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    }, {
      field: 'handles',
      title: '操作项',
      width: 140,
      fixed: 'right',
      cell: (row) => {
        if (row.counting) {
          return {
            btn: [{
              title: row.loading ? '保存中...' : '保存',
              click: () => {
                if (!row.loading && this.vm.onValidateInput(row)) {
                  row.loading = true
                  this.vm.onInit()
                  this.vm.saveCount(row).catch(() => {
                    row.loading = false
                    this.vm.onInit()
                  })
                }
              }
            }, {
              title: '取消',
              visible: !row.loading,
              click: () => {
                row.counting = false
                this.vm.onInit()
              }
            }]
          }
        } else {
          return {
            btn: [{
              title: '结算',
              code: '0206010301',
              click: () => {
                row.counting = true
                row.channelPrice = row.channelCost
                row.countPrice = 0
                this.vm.onInit()
              }
            }, {
              title: '结算记录',
              event: 'onCountLog',
              code: '0206010302',
              visible: this.status != '1'
            }]
          }
        }
      }
    })
  }
}

export default Column